package com.hk.servlet;

import com.alibaba.fastjson.JSONObject;
import com.hk.model.News;
import com.hk.model.Product;
import com.hk.service.INewsService;
import com.hk.service.IProductService;
import com.hk.service.impl.NewsServiceImpl;
import com.hk.service.impl.ProductServiceImpl;
import com.hk.utils.CharacterEncoding;
import com.hk.utils.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhe
 * @Date: 2021/6/24 19:44
 * @Statement:
 */
@WebServlet("/ProductFindById")
public class ProductFindByIdServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        CharacterEncoding.setCharacterEncodingResp(req,resp,"UTF-8");
        IProductService iProductService = new ProductServiceImpl();
        String id = Tool.nullToString(req.getParameter("id"));
        int intId = Tool.parseInt(id);
        List<Product> productList = iProductService.findById(intId);
        Map<String,Object> map = new HashMap<>();
        map.put("status",1);
        map.put("message","success");
        map.put("productList",productList);
        PrintWriter out = resp.getWriter();
        String mapJSON= JSONObject.toJSONString(map);
        out.println(mapJSON);
        out.flush();
        out.close();

    }
}
